import { animation, AnimationClip, AudioClip, error, JsonAsset, Prefab, resources, SpriteFrame, TextAsset } from "cc";


export class ResourcesUtil {
    private static audioCache: Map<string, AudioClip> = new Map(); // 音频缓存
    // 
    static loadResources(url: string, callBack: Function) {
        resources.load(url, (err: Error, res: any) => {
            if (err) {
                error(err.message);
                callBack && callBack(res, err);
                return
            }
            callBack && callBack(res, null);
        });
    }

    /**
     * 异步加载resources中的prefab
     * @param prefabUrl 动态加载预制体的路径
     * @returns 返回Promise结果，成功就携带此prefab
     */
    static loadPrefabs(prefabUrl: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.loadResources(prefabUrl, (res: any, err: Error) => {
                if (err) {
                    reject();
                } else {
                    resolve(res as Prefab);
                }
            })
        })
    }

    /**
     * 异步加载resources中的动画
     * @param url 
     * @returns 
     */
    static loadAnimation(url: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.loadResources(url, (res: any, err: Error) => {
                if (err) {
                    reject && reject(err);
                } else {
                    resolve && resolve(res as AnimationClip);
                }
            })
        })
    }

    static loadText(url: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.loadResources(url, (res: any, err: Error) => {
                if (err) {
                    reject && reject(err);
                } else {
                    resolve && resolve(res as TextAsset);
                }
            })
        })
    }
    /**
     * 异步加载spriteFrame
     * @param url 路径
     * @returns SpriteFrame
     */
    static loadSpriteFrame(url: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.loadResources(url, (res: any, err: Error) => {
                if (err) {
                    reject && reject(err);
                } else {
                    resolve && resolve(res as SpriteFrame);
                }
            })
        })
    }

    /**
     * 
     * @param url 音频路径
     * @returns 音频Clip AudioClip
     */
    static async loadAudioClip(url: string): Promise<AudioClip> {
        // return new Promise((resolve, reject) => {
        //     this.loadResources(url, (res: any, err: Error) => {
        //         if (err) {
        //             reject && reject(err);
        //         } else {
        //             resolve && resolve(res as AudioClip);
        //         }
        //     })
        // })

        // 检查缓存中是否已有该资源
        if (this.audioCache.has(url)) {
            return await this.audioCache.get(url)!;  // 返回缓存中的音频资源
        }

        // 如果缓存中没有该资源，则动态加载
        return new Promise((resolve, reject) => {
            resources.load(url, AudioClip, (err, clip) => {
                if (err) {
                    reject(err);
                    return;
                }
                // 加载成功后将音频资源保存到缓存中
                this.audioCache.set(url, clip);
                resolve(clip);
            });
        });
    }
}